package com.hanlyjiang.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.hanlyjiang.library.R;
import com.hanlyjiang.library.components.BaseCommonLinearLayout;
import com.hanlyjiang.library.widget.color.ColorCircleSelectorView;

/**
 * 颜色选择条
 *
 * @author hanlyjiang on 2017/6/12-18:44.
 * @version 1.0
 */

public class ColorSelectBar extends BaseCommonLinearLayout {

    private ColorCircleSelectorView currentColorCircleSelectorView;

    private int[] colors;
    private OnColorSelected onColorSelected;
    private int itemPadding = 0;
    private int itemMargin = 0;
    private int itemWidth = 0;

    private int itemHoleColor = Color.BLACK;

    public ColorSelectBar(Context context) {
        super(context);
    }

    public ColorSelectBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ColorSelectBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ColorSelectBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray a = retrTypedArray(context, attrs, R.styleable.ColorSelectBar);

        itemMargin = a.getDimensionPixelOffset(R.styleable.ColorSelectBar_itemMargin, 10);
        itemWidth = a.getDimensionPixelOffset(R.styleable.ColorSelectBar_itemWidth, 40);
        itemPadding = a.getDimensionPixelOffset(R.styleable.ColorSelectBar_itemPadding, 10);
        itemHoleColor = a.getColor(R.styleable.ColorSelectBar_itemHoleColor, Color.BLACK);
        int colorRes = a.getResourceId(R.styleable.ColorSelectBar_colorList,0);
        colors = getContext().getResources().getIntArray(colorRes);

        setColorList(colors);

        a.recycle();
    }

    public void setColorList(int[] colors) {
        if (colors == null) {
            return;
        }
        ColorCircleSelectorView singleView = null;
        int index = 0;
        for (int color : this.colors) {
            singleView = new ColorCircleSelectorView(getContext());
            singleView.setColor(color);
            singleView.setHoleColor(itemHoleColor);
            singleView.setSelected(index == 0);
            singleView.setPadding(itemPadding, itemPadding, itemPadding, itemPadding);
            if (index == 0) {
                currentColorCircleSelectorView = singleView;
            }
            addSingleView(singleView);
            index++;
        }
    }

    /**
     * 选中一个颜色时的监听
     */
    public interface OnColorSelected {
        void onColorSelected(int color);
    }


    /**
     * 设置选中某个颜色时的监听
     *
     * @param onColorSelected
     */
    public void setOnColorSelected(OnColorSelected onColorSelected) {
        this.onColorSelected = onColorSelected;
    }

    /**
     * 获取当前选中的颜色
     *
     * @return
     */
    public int getCurrentSelectColor() {
        return currentColorCircleSelectorView.getColor();
    }


    private void addSingleView(ColorCircleSelectorView singleView) {

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(itemWidth, itemWidth);
        layoutParams.setMargins(itemMargin, itemMargin, itemMargin, itemMargin);
        singleView.setLayoutParams(layoutParams);

        singleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View circleView) {
                if (circleView.isSelected()) {
                    return;
                }
                unselectOthers();
                circleView.setSelected(true);
                currentColorCircleSelectorView = (ColorCircleSelectorView) circleView;
                if (onColorSelected != null) {
                    onColorSelected.onColorSelected(((ColorCircleSelectorView) circleView).getColor());
                }
            }
        });
        addView(singleView);
    }

    private void unselectOthers() {
        currentColorCircleSelectorView.setSelected(false);
    }
}
